package com.hmall.search.web;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.hmall.common.dto.PageDTO;
import com.hmall.search.dto.Item;
import com.hmall.search.dto.ItemDto;
import com.hmall.search.dto.SearchVo;
import com.hmall.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description SearchController
 * @Author dmx
 * @Date 2024-11-14
 */
@RestController
@RequestMapping("/search")
@Slf4j
public class SearchController {

    @Autowired
    private SearchService searchService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 过滤项
     * @param itemDto
     * @return
     */
    @PostMapping("/filters")
    public SearchVo searchFilters(@RequestBody ItemDto itemDto){
        SearchVo searchVo =searchService.searchFilters(itemDto);
        return searchVo;
    }

    /**
     * 分页查询
     * @param itemDto
     * @return
     */
    @PostMapping("/list")
    public PageDTO list(@RequestBody ItemDto itemDto){
//        Integer num=1;
//        if (num.equals(itemDto.getPage())&&BeanUtil.isNotEmpty(redisTemplate.opsForValue().get("itemList::"))){
//            //将缓冲中的数据转换为字符串
//            List<Item> list = JSONUtil.toList(redisTemplate.opsForValue().get("itemList::"), Item.class);
//            return new PageDTO<>(20L,list);
//        }
        PageDTO<Item> pageDTO=searchService.list(itemDto);
        return pageDTO;
    }




}
